package com.study.res.controller;

import com.study.res.comm.BaseResponse;
import com.study.res.enums.ErrorEnum;
import com.study.res.exception.WebException;
import com.study.res.pojo.User;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserController
 * @Author swj
 * @Date 2020/10/10 10:08
 */
@RestController
@RequestMapping("/test")
public class UserController  {

    @GetMapping(value = "/exception")
    public BaseResponse get(@RequestParam Integer flag) throws WebException{
        if (flag == 0){
            throw new WebException("my test");
        }else if(flag == 1){
            return BaseResponse.retOk();
        } else if(flag ==2){
            User u = new User();
            u.setPassword("swj");
            u.setUserName("swj");
            return BaseResponse.retOk(u);
        }else {
            return BaseResponse.retFail(ErrorEnum.DUPLICATE_USER_CODE);
        }
    }

    @PostMapping(value = "/test")
    public BaseResponse get(@RequestBody User user) throws WebException{
        //假设项目中出现异常，解决相关异常的同时，抛出自己定义的异常
        try{
            System.out.println(user.toString());
        }catch (Exception e){
            throw new WebException(e.toString());
        }
        return BaseResponse.retOk();
    }
}
